//
// Created by draw on 19-5-11.
//

#include "Comm.h"

Comm Com;

extern "C"
void USART1_IRQHandler(void)
{

    USART1->Clean(Status_Idle);

    Com.Wp = COM_RX_SIZE - (DMA1->DS5.NDTR);

}

void Comm::SetUp(void)
{
    Lp = 0;
    Wp = 0;
    Rp = 0;
    Len = 0;

    Ser=&Serial1;
    Ser->SetUpRxDma(Rxb,COM_RX_SIZE);
}


void Comm::Flush()
{
    Txb[Len]=0;
    Ser->UART->PrintLn(Txb);
    Len = 0;
}

void Comm::Message(const char *s)
{
    Ser->UART->PrintLn(s);
}

void Comm::ReadLine()
{
    while (Rp != Wp)
    {

        if (Rxb[Rp] == '\n')
        {
            Rp++;
            if (Rp == COM_RX_SIZE)
            {
                Rp = 0;
            }
            break;
        }
        Lin[Lp++] = Rxb[Rp++];
        if (Rp == COM_RX_SIZE)
        {
            Rp = 0;
        }
    }

    Lin[Lp++]='\0';

}


